/* 
 Native FullScreen JavaScript API
 -------------
 Assumes Mozilla naming conventions instead of W3C for now
 */
(function() {
    var
            fullScreenApi = {
        supportsFullScreen: false,
        isFullScreen: function() {
            return false;
        },
        requestFullScreen: function() {
        },
        cancelFullScreen: function() {
        },
        fullScreenEventName: '',
        prefix: ''
    },
    browserPrefixes = 'webkit moz o ms khtml'.split(' ');

    // check for native support
    if (typeof document.cancelFullScreen != 'undefined') {
        fullScreenApi.supportsFullScreen = true;
    } else {
        // check for fullscreen support by vendor prefix
        for (var i = 0, il = browserPrefixes.length; i < il; i++) {
            fullScreenApi.prefix = browserPrefixes[i];
            if (typeof document[fullScreenApi.prefix + 'CancelFullScreen' ] != 'undefined') {
                fullScreenApi.supportsFullScreen = true;

                break;
            }
        }
    }

    // update methods to do something useful
    if (fullScreenApi.supportsFullScreen) {
        fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';

        fullScreenApi.isFullScreen = function() {
            switch (this.prefix) {
                case '':
                    return document.fullScreen;
                case 'webkit':
                    return document.webkitIsFullScreen;
                default:
                    return document[this.prefix + 'FullScreen'];
            }
        }
        fullScreenApi.requestFullScreen = function(el) {
            if (browserDetect[0] == "Chrome") {
                el.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
            } else if (browserDetect[0] == "Safari") {
                el.webkitRequestFullScreen();
            } else {
                (this.prefix === '') ? el.requestFullScreen() : el[this.prefix + 'RequestFullScreen']();
            }
        }
        fullScreenApi.cancelFullScreen = function(el) {
            return (this.prefix === '') ? document.cancelFullScreen() : document[this.prefix + 'CancelFullScreen']();
        }
    }

    // jQuery plugin
    if (typeof jQuery != 'undefined') {
        jQuery.fn.requestFullScreen = function() {
            return this.each(function() {
                var el = jQuery(this);
                if (fullScreenApi.supportsFullScreen) {
                    fullScreenApi.requestFullScreen(el);
                }
            });
        };
    }
    // export api
    window.fullScreenApi = fullScreenApi;
})();
function isFullScreen() {
    var res = false;
    if (window.fullScreenApi.supportsFullScreen) {
        if (window.fullScreenApi.isFullScreen()) {
            res = true;
        }
    }
    return res;
}
function initFullScreen() {
    if (window.fullScreenApi.supportsFullScreen) {
        var fsElement = document.getElementById('ultBody');
        var fsButton = document.getElementById('fsbutton');
        fsButton.addEventListener('click', function() {
            if (!window.fullScreenApi.isFullScreen()) {
                window.fullScreenApi.requestFullScreen(fsElement);
            } else {
                window.fullScreenApi.cancelFullScreen(fsElement);
            }
        }, true);
    }
}
function fullScreen() {
    if (window.fullScreenApi.supportsFullScreen) {
        var fsElement = document.getElementById('ultBody');
        if (!window.fullScreenApi.isFullScreen()) {
            window.fullScreenApi.requestFullScreen(fsElement);
        } else {
            window.fullScreenApi.cancelFullScreen(fsElement);
        }
    }
}
function cancelFullScreen() {
    if (window.fullScreenApi.supportsFullScreen) {
        var fsElement = document.getElementById('ultBody');
        if (window.fullScreenApi.isFullScreen()) {
            window.fullScreenApi.cancelFullScreen(fsElement);
        }
    }
}